<template>
  <view class="taskListChild">
    <pitHeader :title="title" :showSearch="true"></pitHeader>
    <view class="child-nav">
      <view v-for="(tab,index) in tabs" :key="index" :class="['child-nav-item', { active: activeTab === index }] "
        @click="activeTab = index">
        {{tab}}
      </view>
    </view>
    <view class="child-content">
      <view v-for="(task,taskIndex) in filteredTasks" :key="taskIndex" class="child-content-task">
        <view v-if="task.isPc" class="pc-tag">电脑端</view>
        <view class="task-title">{{task.title}}</view>
        <view class="task-info">
          <view class="task-info-element">
            <text class="element-text">所属流程：</text>
            <view class="element-value">{{ task.bpmnName }}</view>
          </view>
          <view class="task-info-element">
            <text class="element-text">发起人：</text>
            <view class="element-value">{{ task.startUserName }}</view>
          </view>
          <view class="task-info-element">
            <text class="element-text">发起时间：</text>
            <view class="element-value">{{  task.startTime}}</view>
          </view>
        </view>
        <button v-if="task.canHandle" class="task-btn" @click="handleTask(task)">
          处理任务
        </button>
      </view>
    </view>
  </view>
</template>

<script>
  import{ apiBpmTaskRunTaskCustomTaskListTotal } from '@/api/bpm/task.js'
  export default {
    components:{
      pitHeader
    },
    data() {
      return {
        tabs: ['待办任务', '已办任务', '我发起的'],
        activeTab: 0,
        tasks: [],
        enCode:'',
        title:''
      }
    },
    onLoad(options) {
      if (options.title) {
        this.title = decodeURIComponent(options.title);
      }
      this.enCode = options.enCode;
      this.init();
    },
    computed: {
      filteredTasks() {
        if (this.activeTab === 0) {
          return this.tasks.filter(task => task.canHandle);
        } else if (this.activeTab === 1) {
          return this.tasks.filter(task => !task.canHandle);
        } else {
          return this.tasks;
        }
      }
    },
    methods: {
      init(){
        apiBpmTaskRunTaskCustomTaskListTotal({
          category: this.enCode,
          pageSize: 20,
          pageNumber: 1
        }).then(res=>{
          if(res.code === '00000'){
              this.tasks = Array.isArray(res.data.list) ? res.data.list : [];
          } else {
            uni.showToast({
              title: '数据获取失败',
              icon: 'none'
            });
            this.tasks = [];
          }
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
  .taskListChild {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: rgb(240, 245, 252);

    .child-nav {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 12px 30px;
      background-color: white;

      .child-nav-item {
        font-weight: 550;
        color: rgb(102, 102, 102);
      }

      .child-nav-item.active {
        color: #0f519c;
        border-bottom: 2px solid #0f519c;
      }
    }
    .child-content {
      flex: 1;
      overflow-y: auto;
      padding: 10px 15px;
      .child-content-task {
        background-color: #fff;
        border-radius: 8px;
        padding: 10px;
        margin-bottom: 10px;
        .pc-tag {
          display: inline-block;
          background-color: #67c23a;
          color: #fff;
          font-size: 12px;
          padding: 2px 5px;
          border-radius: 4px;
          margin-bottom: 5px;
        }
        .task-title{
          // display: flex;
          // align-items: center;
          // .task-date {
            font-size: 18px;
            font-weight: 350;
          // }
          // .task-name {
          //   font-size: 18px;
          //   font-weight: 350;
          // }
        }
        .task-info {
          display: flex;
          flex-direction: column;

          .task-info-element{
            display: flex;
            align-items: center;
            margin: 8px 0;
            .element-text{
              font-size: 16px;
              font-weight: 350;
            }
            .element-value{
              font-size: 16px;
              color:#666;
            }
          }
        }
        .task-btn {
          margin-right: 10px;
          width: 150px;
          font-size: 15px;
          border: none;
          border-radius: 50px;
          color: white;
          background-color: rgb(2,126,251);
        }
      }
    }
  }
</style>
